<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        form {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 300px;
        }

        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }

        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 15px;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
        }

        input[type="submit"] {
            width: 100%;
            padding: 10px;
            background-color: #007BFF;
            border: none;
            border-radius: 4px;
            color: white;
            font-size: 16px;
            cursor: pointer;
        }

        input[type="submit"]:hover {
            background-color: #0056b3;
        }

        .error {
            color: red;
            font-size: 14px;
            margin-top: -10px;
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <form action="register.php" method="post" id="myForm">
        <div>
            <label for="username">用户名</label>
            <input type="text" name="username" id="username" placeholder="请输入用户名" required/>
            <label for="password">密码</label>
            <input type="password" name="password" id="password" placeholder="请输入密码" required/>
            <label for="repassword">确认密码</label>
            <input type="password" name="repassword" id="repassword" placeholder="请再次输入密码" required/>
            <div id="error" class="error"></div>
            <input type="submit" value="注册">
        </div>
    </form>

    <script>
        // 确保 DOM 完全加载后再执行 JavaScript
        document.addEventListener("DOMContentLoaded", function() {
            // 做密码校验，两次不一致阻止提交
            var pwd = document.getElementById("password");
            var repwd = document.getElementById("repassword");
            var errorDiv = document.getElementById("error");

            repwd.onblur = function() {
                if (pwd.value !== repwd.value) {
                    errorDiv.textContent = "两次密码不一致";
                    repwd.focus();
                } else {
                    errorDiv.textContent = "";
                }
            };

            document.getElementById("myForm").onsubmit = function(event) {
                if (pwd.value !== repwd.value) {
                    event.preventDefault(); // 阻止表单提交
                    errorDiv.textContent = "两次密码不一致";
                    repwd.focus();
                    return false;
                }
                return true;
            };
        });
    </script>
</body>
</html>